package com.student.Activitys.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.student.Activitys.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * 项目名称：智慧校园学生端
 * 类描述：周课表 节次 adapter
 * 创建人：张飞祥
 * 创建时间：2017/9/25 15:32
 * 修改人：张飞祥
 * 修改时间：2017/9/25 15:32
 * 修改备注：
 */
public class ScheduleSectionAdapter extends BaseAdapter{


    private Context context;
    private List<Boolean> list;
    private LayoutInflater inflater = null;

    public ScheduleSectionAdapter(Context context, List<Boolean> list) {
        this.context = context;
        this.list = list;
        this.inflater= LayoutInflater.from(context);  //这里就是确定你listview在哪一个layout里面展示
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_schedule_section, null);
            //把convertView中的控件保存到viewHolder中
            holder.numberTop = (TextView)convertView .findViewById(R.id.item_schedule_section_number_top);
            holder.numberBottom = (TextView)convertView .findViewById(R.id.item_schedule_section_number_bottom);
            holder.timeTop = (TextView)convertView .findViewById(R.id.item_schedule_section_time_top);
            holder.timeBottom = (TextView)convertView .findViewById(R.id.item_schedule_section_time_bottom);
            holder.spotTop = (ImageView)convertView .findViewById(R.id.item_schedule_section_spot_top);
            holder.spotBottom = (ImageView)convertView .findViewById(R.id.item_schedule_section_spot_bottom);
            //通过setTag将ViewHolder与convertView绑定
            convertView.setTag(holder);
        }else{
            //通过调用缓冲视图convertView，然后就可以调用到viewHolder,viewHolder中已经绑定了各个控件，省去了findViewById的步骤
            holder = (ViewHolder)convertView.getTag();
        }
        //设置 节次名
        holder.numberTop.setText((i*2+1)+"");
        holder.numberBottom.setText((i*2+2)+"");
        /**
         * 判断 是上午还是下午
         */
        if (i>1){
            holder.timeTop.setText("PM");         //显示 下午
            holder.timeBottom.setText("PM");      //显示下午
        }else{
            holder.timeTop.setText("AM");         //显示上午
            holder.timeBottom.setText("AM");      //显示上午
        }

        //判断当天 每节 是否有课
        if (list.get(i) == false){
            holder.spotTop.setBackgroundResource(R.drawable.item_day_schedule_spot_no_icon);          //显示 绿点
            holder.spotBottom.setBackgroundResource(R.drawable.item_day_schedule_spot_no_icon);       //显示 绿点
        }else {
            holder.spotTop.setBackgroundResource(R.drawable.item_day_schedule_spot_yes_icon);         //显示 红点
            holder.spotBottom.setBackgroundResource(R.drawable.item_day_schedule_spot_yes_icon);      //显示 红点
        }
        AutoUtils.auto(convertView);
        return convertView;
    }

    static class ViewHolder{
        public TextView numberTop;
        public TextView numberBottom;
        public TextView timeTop;
        public TextView timeBottom;
        public ImageView spotTop;
        public ImageView spotBottom;
    }
}
